package com.leo.quartz.repository;

import com.leo.quartz.common.util.UserTest;
import com.leo.quartz.entity.GroupPO;
import com.leo.quartz.entity.UserPO;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * @author liusl
 * @version 1.0
 * @date 2018/12/24 20:59
 */
public abstract class AbstractDao<T> implements BatchDao<T> {
    @PersistenceContext
    private EntityManager em;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(List<UserTest> list) {
        for (int i = 0; i < list.size(); i++) {
            em.persist(list.get(i).getUser());
            UserPO po = list.get(i).getUser();
            GroupPO groupPO = list.get(i).getGroupPO();
            groupPO.setUserId(po.getId().longValue());
            em.persist(groupPO);
            if (i % 200 == 0) {
                em.flush();
                em.clear();
            }
        }

    }
}
